package com.regvision.contacts.dialog;

import com.regvision.contacts.R;
import com.regvision.contacts.domain.ContactDataModel;
import com.regvision.contacts.ui.adapter.EMailAdapter;
import com.regvision.contacts.ui.adapter.PhoneAdapter;
import com.regvision.contacts.ui.adapter.PhoneAdapter.PhoneType;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

public class ContactDialog extends BaseDialog {

	private ContactDataModel mModel;

	private LinearLayout llPhone;
	private LinearLayout llMail;
	
	private ListView mPhoneView;
	private ListView mEMailView;
	
	private PhoneAdapter mPhoneAdapter;
	private EMailAdapter mEMailAdapter;
	
	public ContactDialog(Context context) {
		super(context);
	}

	public ContactDialog(Context context, int theme) {
		super(context, theme);
	}

	protected ContactDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}
	
	@Override
	protected void init() {
		// TODO Auto-generated method stub
		super.init();
		update();
	}
	
	private void update() {
		if (mModel == null) return;
		
		View view = View.inflate(getContext(), R.layout.dialog_contact, null);
		
		llPhone = (LinearLayout) view.findViewById(R.id.ll_dialog_contact_phone);
		llMail = (LinearLayout) view.findViewById(R.id.ll_dialog_contact_mail);
		
		mPhoneView = (ListView) view.findViewById(R.id.lv_dialog_contact_phone);
		mEMailView = (ListView) view.findViewById(R.id.lv_dialog_contact_email);

		mPhoneAdapter = new PhoneAdapter(getContext(), PhoneType.NoPhone, R.layout.item_phone, mModel.phones);
		mEMailAdapter = new EMailAdapter(getContext(), R.layout.item_email, mModel.emails);
		
		// 没有任何号码
		if (mPhoneAdapter.getCount() == 0) {
			llPhone.setVisibility(View.GONE);
		} 
		// 没有任何EMail
		if (mEMailAdapter.getCount() == 0) {
			llMail.setVisibility(View.GONE);
		}
		
		mPhoneView.setAdapter(mPhoneAdapter);
		mEMailView.setAdapter(mEMailAdapter);
		
		mLlContent.addView(view);
	}

	public void setContactDataModel(ContactDataModel model) {
		this.mModel = model;
		update();
	}

}
